<?php

namespace App\Http\Controllers\App;

use App\Model\SmsMsg;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class SettingController extends Controller
{
	/**
	 * @var User
	 */
	private $user;

	public function __construct()
	{
		$this->user = Auth::user();
	}

	//改密
	public function resetPwd(Request $request, SmsMsg $sms)
	{
		$this->validate($request, [
			'code' => 'required',
			'password' => 'required'
		], [
			'code.required' => '验证码不能为空',
			'password.required' => '新密码不能为空'
		]);

		$sms->validate($this->user->phone, $request->input("code"), 3);

		$this->user->password = bcrypt($request->input('password'));

		if (!$this->user->save()) {
			return $this->fail("密码修改失败");
		}
		return $this->success();
	}

	//验证旧手机
	public function validateOldPhone(Request $request, SmsMsg $sms)
	{
		$this->validate($request, [
			'code' => 'required',
		], [
			'code.required' => '验证码不能为空',
		]);

		$code = $request->input("code");

		$sms->validate($this->user->phone, $code, 4);

		//验证通过，写入临时
		$hash = md5("{$this->user->id}-" . time());

		Cache::put('validate_old_phone-' . $this->user->id, $hash, 5);

		return $this->renderJson(['sign'=>$hash]);
	}

	//绑定新手机
	public function changePhone(Request $request, SmsMsg $sms)
	{
		$this->validate($request, [
			'code' => 'required',
			'phone' => [
				'required',
				'regex:/^1[345789][0-9]{9}$/',
				'unique:users,phone'
			],
			'sign'=>'required'
		],[
			'phone.required' => '手机号不能为空',
			'phone.regex' => '手机号格式错误',
			'phone.unique' => '该手机已被使用',
			'code.required'=>'短信验证码不能为空',
			'sign.required'=>'签名不能为空'
		]);

		$phone = $request->input("phone");

		$hash = Cache::get('validate_old_phone-'.$this->user->id);
		if ($request->input('sign') != $hash){
			return $this->fail('签名错误');
		}

		$sms->validate($phone, $request->input("code"), 4);

		$this->user->phone = $phone;

		if (!$this->user->save()) {
			return $this->fail('手机更换失败');
		}

		return $this->success();
	}
	//验证旧支付密码
	public function validatePaymentPwd(Request $request)
	{
		$this->validate($request, [
			'password' => 'required'
		],[
			'password.required' => '密码不能为空',
		]);

		if (!password_verify($request->input('password'), $this->user->payment_password)){
			return $this->fail('原支付密码错误');
		}
		//验证通过，写入临时
		$hash = md5("{$this->user->id}-" . time());

		Cache::put('validate_payment_pwd-' . $this->user->id, $hash, 5);
		return $this->renderJson(['sign'=>$hash]);
	}
	//设置新密码
	public function setPymPwd(Request $request)
	{
		$this->validate($request, [
			'sign' => 'required',
			'password' => 'required'
		],[
			'sign.required' => '签名不能为空',
			'password.required' => '新支付密码不能为空'
		]);
		$hash = Cache::get('validate_payment_pwd-'.$this->user->id);
		if ($request->input('sign') != $hash){
			return $this->fail('签名错误');
		}
		$this->user->payment_password = bcrypt($request->input('password'));

		if (!$this->user->save()) {
			return $this->fail("支付密码修改失败");
		}
		return $this->success();
	}
	//忘记支付密码
	public function validatePaymentSms(Request $request, SmsMsg $sms)
	{
		$this->validate($request, [
			'code' => 'required'
		], [
			'code.required'=>'短信验证码不能为空'
		]);
		$code = $request->input('code');

		$sms->validate($this->user->phone, $code, 5);

		//验证通过，写入临时
		$hash = md5("{$code}-{$this->user->id}-".time());

		Cache::put('validate_payment_pwd-'.$this->user->id, $hash,5);

		return $this->renderJson(['sign'=>$hash]);
	}
}
